<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<i @click="togoBack">&#8249;</i>
			<p>购物车</p>
		</header>
		<ul class="food">
			<li v-for="(item,index) in foodArr" :key="item.foodId" v-show="item.quantity>0">

				<div class="food-left">
					<img :src="item.foodImg">
					<div class="food-left-info">
						<h3>{{item.foodName}}</h3>
						<p>{{item.foodExplain}}</p>
						<p>&#165;{{item.foodPrice}}</p>
					</div>
				</div>
				<div class="food-right">
					<div>
						<i class="fa fa-minus-circle" @click="minus(index)" v-show="item.quantity!=0"></i>
					</div>
					<p><span v-show="item.quantity!=0">{{item.quantity}}</span></p>
					<div>
						<i class="fa fa-plus-circle" @click="add(index)"></i>
					</div>
				</div>
			</li>
		</ul>
	</div>
</template>

<script>
	export default {
		name: 'Cart',
		data() {
			return {
				businessId: this.$route.query.businessId,
				business: {},
				foodArr: [],
				user: {}
			}
		},
		created() {
			this.user = this.$getSessionStorage('user');
			//根据businessId查询商家信息
			this.$axios.get('Business/BusinessId', {
				params: {
					businessId: this.businessId
				}
			}).then(response => {
				this.business = response.data.result;
			}).catch(error => {
				console.error(error);
			});
			//根据businessId查询所属食品信息
			this.$axios.get('Food/FoodList', {
					params: {
						businessId: this.businessId
					}
				})
				.then(response => {
					this.foodArr = response.data.result;
					for (let i = 0; i < this.foodArr.length; i++) {
						this.foodArr[i].quantity = 0;
					}
					//如果已登录，那么需要去查询购物车中是否已经选购了某个食品
					if (this.user != null) {
						this.listCart();
					}
				}).catch(error => {
					console.error(error);
				});
		},
		methods: {
			togoBack() {
				// 返回上一页
				this.$router.go(-1);
			},
			listCart() {
				this.$axios.get('Cart/CartList', {
					params: {
						businessId: this.businessId,
						userId: this.user.userId
					}
				}).then(response => {
					let cartArr = response.data.result;
					//遍历所有食品列表
					for (let foodItem of this.foodArr) {
						foodItem.quantity = 0;
						for (let cartItem of cartArr) {
							if (cartItem.foodId == foodItem.foodId) {
								foodItem.quantity = cartItem.quantity;
							}
						}
					}
					this.foodArr.sort();
				}).catch(error => {
					console.error(error);
				});
			},

		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;

	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		align-items: center;
	}

	.wrapper header i {
		font-size: 10vw;
		user-select: none;
	}

	.wrapper header p {
		margin-left: 35vw;
	}

	.wrapper .food {
		width: 100%;
		/*使用上外边距避开header部分*/
		margin-top: 12vw;
	}

	.wrapper .food li {
		width: 100%;
		box-sizing: border-box;
		padding: 2.5vw;
		user-select: none;

		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .food li .food-left {
		display: flex;
		align-items: center;
	}

	.wrapper .food li .food-left img {
		width: 20vw;
		height: 20vw;
	}

	.wrapper .food li .food-left .food-left-info {
		margin-left: 3vw;
	}

	.wrapper .food li .food-left .food-left-info h3 {
		font-size: 3.8vw;
		color: #555;
	}

	.wrapper .food li .food-left .food-left-info p {
		font-size: 3vw;
		color: #888;
		margin-top: 2vw;
	}

	.wrapper .food li .food-right {
		width: 16vw;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .food li .food-right .fa-minus-circle {
		font-size: 5.5vw;
		color: #999;
		cursor: pointer;
	}

	.wrapper .food li .food-right p {
		font-size: 3.6vw;
		color: #333;
	}

	.wrapper .food li .food-right .fa-plus-circle {
		font-size: 5.5vw;
		color: #0097EF;
		cursor: pointer;
	}
</style>